{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### ❇️ Adding new columns: 🐼 df.assign(**kwargs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Returns a new dataframe with all original columns in addition to new ones. <br>\n",
    "kwargs: <br>\n",
    "- The column names are keywords. If the values are callable, they are computed on the DataFrame <br>\n",
    "and assigned to the new columns. <br>\n",
    "- If the values are not callable, (e.g. a Series, scalar, or array), <br>\n",
    "they are simply assigned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_celcius</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Delhi</th>\n",
       "      <td>35.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mumbai</th>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        temp_celcius\n",
       "Delhi           35.0\n",
       "Mumbai          25.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'temp_celcius': [35.0, 25.0]}, index=['Delhi', 'Mumbai'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_celcius</th>\n",
       "      <th>wind_speed_kmph</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Delhi</th>\n",
       "      <td>35.0</td>\n",
       "      <td>26.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mumbai</th>\n",
       "      <td>25.0</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        temp_celcius  wind_speed_kmph\n",
       "Delhi           35.0             26.0\n",
       "Mumbai          25.0             31.0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Let's add a new column wind_speed_kmph; where we take values from a list\n",
    "speed_values = [26.0, 31.0]\n",
    "df = df.assign(wind_speed_kmph=speed_values) # kmph: kilometers per hour\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_celcius</th>\n",
       "      <th>wind_speed_kmph</th>\n",
       "      <th>wind_speed_mph</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Delhi</th>\n",
       "      <td>35.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>16.146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mumbai</th>\n",
       "      <td>25.0</td>\n",
       "      <td>31.0</td>\n",
       "      <td>19.251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        temp_celcius  wind_speed_kmph  wind_speed_mph\n",
       "Delhi           35.0             26.0          16.146\n",
       "Mumbai          25.0             31.0          19.251"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Using a callable to calculate values of new column based on existing column values\n",
    "df = df.assign(wind_speed_mph = lambda x: x['wind_speed_kmph']*0.621) # mph: miles per hour\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_celcius</th>\n",
       "      <th>wind_speed_kmph</th>\n",
       "      <th>wind_speed_mph</th>\n",
       "      <th>temp_fahrenheit</th>\n",
       "      <th>temp_kelvin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Delhi</th>\n",
       "      <td>35.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>16.146</td>\n",
       "      <td>95.0</td>\n",
       "      <td>308.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mumbai</th>\n",
       "      <td>25.0</td>\n",
       "      <td>31.0</td>\n",
       "      <td>19.251</td>\n",
       "      <td>77.0</td>\n",
       "      <td>298.15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        temp_celcius  wind_speed_kmph  wind_speed_mph  temp_fahrenheit  \\\n",
       "Delhi           35.0             26.0          16.146             95.0   \n",
       "Mumbai          25.0             31.0          19.251             77.0   \n",
       "\n",
       "        temp_kelvin  \n",
       "Delhi        308.15  \n",
       "Mumbai       298.15  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You can create multiple columns within the same assign \n",
    "# where one of the columns depends on another one defined within the same assign 💥 \n",
    "df = df.assign(temp_fahrenheit=lambda x: x['temp_celcius'] * 9 / 5 + 32, temp_kelvin=lambda x: (x['temp_fahrenheit'] +  459.67) * 5 / 9)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### ❇️ Hope you enjoyed reading!! 📖 \n",
    "##### ❇️ follow → @akshay_pachaar  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
